<?php
class newYearController extends  appController{
	public function index()
	{
		$m = Model('newYear');
		$info = $m->getNewYearActivity();
		$this->assign("info", $info);
		//获取tv红包周边红包
		$hongbao = $m->getTVZBHongbao();
		$this->assign("action", "saveAcitity");
		$this->assign("hongbao", $hongbao);
		$this->display("index.html");	
	}
	
	public function saveAcitity()
	{ 
		$m = Model('newYear');
		$tvActivity = array();
		$zbActivity = array();
		for ($i = 1;$i<8;$i++)
		{
			$info =array();
			$info['zbbegin_time'] = isset($_POST['zbbegin_time'.$i])?strtotime($_POST['zbbegin_time'.$i]):"";
			$zbcontinue_time = isset($_POST['zbcontinue_time'.$i])?intval($_POST['zbcontinue_time'.$i]):0;
			$info['zbend_time'] = $info['zbbegin_time']+($zbcontinue_time*60);
			$info['tvbegin_time'] = isset($_POST['tvbegin_time'.$i])?strtotime($_POST['tvbegin_time'.$i]):"";
			$tvcontinue_time = isset($_POST['tvcontinue_time'.$i])?intval($_POST['tvcontinue_time'.$i]):0;
			$info['tvend_time'] = $info['tvbegin_time']+($tvcontinue_time*60);
			$info['tvhb_id'] = isset($_POST['tvhb_id'.$i])?intval($_POST['tvhb_id'.$i]):0;
			$info['zbhb_id'] = isset($_POST['zbhb_id'.$i])?intval($_POST['zbhb_id'.$i]):0;
			$info['status'] = $i;
			$id = isset($_POST['id'.$i])?intval($_POST['id'.$i]):"";
			if($id){//update
				$result = $m->update($info,array("table"=>"newyear_activity","id"=>$id));
			}else{
				$result = $m->insert($info,"newyear_activity");
			}
			$tv = array(
					"begin_time"=>$info['tvbegin_time'],
					"end_time" => $info['tvend_time']+30,
					"tvhb_id" => $info['tvhb_id'],
					"status" =>$info['status']
			);
			$zb = array(
					"begin_time"=>$info['zbbegin_time'],
					"end_time" => $info['zbend_time']+10,
					"zbhb_id" => $info['zbhb_id'],
					"status" =>$info['status']
			);
			$tvActivity[] = $tv;
			$zbActivity[] = $zb;
		}
		if($result){
			$fpzb = fopen(ROOT_PATH."zbActivity.json", "w");
			fwrite($fpzb, json_encode($zbActivity));
			fclose($fpzb);
			$fptv = fopen(ROOT_PATH."tvActivity.json", "w");
			fwrite($fptv, json_encode($tvActivity));
			fclose($fptv);
			$this->showMsg(array("error"=>0,"content"=>"保存成功"));
		}else{
			$this->showMsg(array("error"=>1,"content"=>"保存失败"));
		}
	}
	
	public function getVote()
	{
		$m = Model('newYear');
		$record = $m->getRecord();
		$this->assign("record", $record);
		$this->display("vote_record.html");
	}
	
}